package cn.edu.lzzy.controllers.course;

import cn.edu.lzzy.entities.Applicant;
import cn.edu.lzzy.entities.Messages;
import cn.edu.lzzy.services.ApplicantService;
import cn.edu.lzzy.utils.StringUtils;

import cn.edu.lzzy.vm.JSONResult;
import cn.edu.lzzy.vm.VmApplicant;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@EnableOpenApi
@Tag(name = "applicant-api-controller",description = "课程申请业务")
public class ApplicantApiController {
    private final ApplicantService service;
    @Autowired
    public ApplicantApiController(ApplicantService service) {
        this.service = service;
    }
    @Operation(summary = "申请课程")
    @PostMapping(value = "/student/applay",produces = "application/json")
    public JSONResult apply(@RequestBody JSONObject json)
    {
        //用户必须成功登录，获取token
        String token=json.getString("token");

        //确定是谁申请，获取学生Id
        String studentId=json.getString("studentId");
        //确定学生申请的课程，获取课程Id
        String courseId=json.getString("courseId");

        if(StringUtils.isEmpty(token)||StringUtils.isEmpty(studentId)||StringUtils.isEmpty(courseId)){
            return JSONResult.isError(400, Messages.LOGIN_FAIL_INFO.toString());
        }
        try {
            UUID stuId=UUID.fromString(studentId);
            UUID cId=UUID.fromString(courseId);
            Applicant applicant=service.apply(stuId,cId,token);
            if(applicant==null){
                return JSONResult.isError(400,Messages.INVALID_FORMAT.toString());
            }
            return JSONResult.isOk(new VmApplicant(applicant),Messages.SUCCESS.toString());

        }catch (Exception ex){
            return JSONResult.isError(400,ex.getMessage());
        }
    }
    private JSONResult optApplicants(List<Applicant> applicants){
        if(applicants==null){
            return JSONResult.isError(403,Messages.NO_PERMISSION.toString());
        }
        List<VmApplicant> vms=new ArrayList<>();
        applicants.forEach(a->vms.add(new VmApplicant(a)));
        return JSONResult.isOk(vms, "共计"+applicants.size()+"条数据"+Messages.SUCCESS.toString());

    }


    @Operation(summary = "查询某个学生的课程申请信息")
    @GetMapping(value = "student/getApplicants")
    public JSONResult getStudentsApplicants(@RequestParam(name = "学生id")UUID studentId,@RequestParam(name="登录令牌")String token){
        /***
         *业务逻辑：
         * 学生查询课程申请：studentId+token
         * 教师查询课程申请：token
         */
        List<Applicant> applicants=service.getAllCourses(studentId,token);
       return optApplicants(applicants);
    }

    @Operation(summary = "查询某个学生的课程申请成功的信息")
    @GetMapping(value = "student/getApprovedApplicants")
    public JSONResult getApprovedApplicants(@RequestParam(name = "学生id")UUID studentId,@RequestParam(name="登录令牌")String token){
       List<Applicant> applicants=service.getApprovedStudents(studentId,token);
        return optApplicants(applicants);
    }
    @Operation(summary = "查询某个学生的课程申请失败的信息")
    @GetMapping(value = "student/getDeclinedApplicants")
    public JSONResult getDeclinedApplicants(@RequestParam(name = "学生id")UUID studentId,@RequestParam(name="登录令牌")String token){
        List<Applicant> applicants=service.getDeclineStudents(studentId,token);
        return optApplicants(applicants);
    }
    //bug
    @Operation(summary = "教师审批课程申请")
    @GetMapping(value = "/teacher/approved")
    public JSONResult approve(@RequestParam(name = "学生申请id")String applicantId,@RequestParam(name="登录令牌")String token){
        if(StringUtils.isEmpty(token)||StringUtils.isEmpty(applicantId)){
            return JSONResult.isError(417,Messages.EXPECTATION_FAILED.toString());
        }
        try {
            UUID aId=UUID.fromString(applicantId);
            Applicant applicant=service.approve(aId,token);//审批通过
            if(applicant==null){
                return JSONResult.isError(403,Messages.NO_PERMISSION.toString());
            }
            return JSONResult.isOk(new VmApplicant(applicant),Messages.APPROVED.toString());
        }catch (Exception ex){
            return JSONResult.isError(417,Messages.EXPECTATION_FAILED.toString());
        }

    }

    ///bug
    @Operation(summary = "教师拒绝课程申请")
    @GetMapping(value = "/teacher/declined")
    public JSONResult decline(@RequestParam(name = "学生申请id")String applicantId,@RequestParam(name="登录令牌")String token) {
        if(StringUtils.isEmpty(token)||StringUtils.isEmpty(applicantId)){
            return JSONResult.isError(417,Messages.EXPECTATION_FAILED.toString());
        }
        try {
            UUID aId=UUID.fromString(applicantId);
            Applicant applicant=service.decline(aId,token);//审批拒绝
            if(applicant==null){
                return JSONResult.isError(403,Messages.NO_PERMISSION.toString());
            }
            return JSONResult.isOk(new VmApplicant(applicant),Messages.REJECTED.toString());
        }catch (Exception ex){
            return JSONResult.isError(417,Messages.EXPECTATION_FAILED.toString());
        }

    }


    @Operation(summary = "查询教师某门课程申请记录")
    @GetMapping(value = "/teacher/getApplyingApplicants")
    public JSONResult getTeacherApplyingApplicants(@RequestParam(name = "课程ID")UUID courseId,@RequestParam(name="登录令牌")String token){
        List<Applicant> applicants=service.getApplyingCourses(courseId,token);
        return optApplicants(applicants);

    }
    @Operation(summary = "查询教师某门课程申请成功记录")
    @GetMapping(value = "/teacher/getApprovedApplicants")
    public JSONResult getTeacherApprovedApplicants(@RequestParam(name = "课程ID")UUID courseId,@RequestParam(name="登录令牌")String token){
        List<Applicant> applicants=service.getApprovedCourses(courseId,token);
        return optApplicants(applicants);
    }

    @Operation(summary = "查询教师某门课程申请失败记录")
    @GetMapping(value = "/teacher/getDeclinedApplicants")
    public JSONResult getTeacherDeclinedApplicants(@RequestParam(name = "课程ID")UUID courseId,@RequestParam(name="登录令牌")String token){
        List<Applicant> applicants=service.getDeclinedCourses(courseId,token);
        return optApplicants(applicants);
    }

    @Operation(summary = "教师移除学生")
    @GetMapping(value = "/teacher/remove")
    public JSONResult remove(@RequestBody JSONObject json){
//        String token=;
//        String courseId=;
//        String studentId=;
//        List<Applicant> applicants=service.removeStudent();
//        return optApplicants(applicants);
        return null;

    }
}
